package com.ruoyi.framework.web.service;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.framework.security.token.ClientUserAuthToken;
import com.ruoyi.system.domian.jpa.CdUserJpa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Dale Mao    ---多多 Email:453918229@qq.com
 * @description ClientUserService
 * @date 2021-01-27 22:27
 */
@Component
public class ClientUserService {

    @Resource
    private TokenService tokenService;

    @Resource
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;




    public String  login(CdUserJpa cdUserJpa)
    {

        // 用户验证
        Authentication authentication = null;

        //会调用自定义的Provide 方法 ClientUserAuthenticationProvider
        authentication = authenticationManager
                .authenticate(new ClientUserAuthToken(cdUserJpa.getPhone()));

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();


        // 生成token
        return  tokenService.createToken(loginUser);

    }
}
